﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using SMSSnew.DATA.CLASSES;

namespace SMSSnew.UI.vicePrincipal.timetable.dataEntry
{
    public partial class vicePrinClassCombinationDetails : System.Web.UI.Page
    {
        classCLZ insertCC;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                timeTableCLZ objTimeTableClass = new timeTableCLZ();
                classCLZ objClassClass = new classCLZ();
                subjectCLZ objSubjClass = new subjectCLZ();

                DataTable dt;
                DataTable dt1;
                dt = objTimeTableClass.loadGrade();
                dt1 = objSubjClass.selectSubjectsWithTypes();

          

                if (dt.Rows.Count != 0)                     //load grades to combo box
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        cboGrade.Items.Insert(i, dt.Rows[i][0].ToString());
                    }
                }

                if (dt1.Rows.Count != 0)                    //load subjects to combo box
                {
                    for (int i = 0; i < dt1.Rows.Count; i++)
                    {
                        
                        cboSubject.Items.Insert(i,dt1.Rows[i][0].ToString());
                        
                    }
                }

                 
                btnAdd.Visible = false;
            }

            




            if (ViewState["falg"] != null)                  // to control dynamic properties
            {
                add_dynamic_control();
            } 

        }

        protected void cboGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
           // PH.Visible = true;
            if (ViewState["falg"] == null)
            {
                add_dynamic_control();
             } 
          
            ViewState.Add("falg", true);
        }

       


        public void add_dynamic_control()                       //create dynamic properties 
        {
            DataTable dt;
            DataTable dt1;
            DataTable dt2;

            classCLZ objClass = new classCLZ(cboGrade.SelectedItem.ToString());
            timeTableCLZ objTT = new timeTableCLZ();

            
            dt = objClass.add_again(cboGrade.SelectedValue.ToString(),cboSubject.SelectedValue.ToString());
            dt1 = objTT.loadClassID(objClass);
            dt2 = objClass.load_class_combination(cboGrade.SelectedValue.ToString(),cboSubject.SelectedValue.ToString());

            if (dt.Rows.Count == 0)
            {
                Table table = new Table();
                table.ID = "fix_table";

                PH.Controls.Add(table);

                TableRow row = new TableRow();
                TableCell cell = new TableCell();
                Label lbl = new Label();


                lbl.ID = "fix_lbl";

                if (dt.Rows.Count == 0 )
                {
                    lbl.Text = "Groups are already added for all classes in grade " + cboGrade.SelectedValue.ToString() + " , for " + cboSubject.SelectedValue.ToString();
                }

                if (dt.Rows.Count == 0 && dt1.Rows.Count == 0)
                {
                    lbl.Text = "There are no classes to combine in this grade";
                }

                PH.Controls.Add(lbl);



                PH.Controls.Add(cell);
                PH.Controls.Add(row);

                btnAdd.Visible = false;
            }

            else
            {
            
                if (dt.Rows.Count != 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        Table table = new Table();
                        table.ID = "tbl_dy" + i;

                        PH.Controls.Add(table);

                        string clas = dt.Rows[i][0].ToString();
                        TableRow row = new TableRow();
                        TableCell cell = new TableCell();
                        Label lbl = new Label();


                        lbl.ID = "lbl_" + i + "";

                        lbl.Text = clas;



                        PH.Controls.Add(lbl);



                        PH.Controls.Add(cell);
                        PH.Controls.Add(row);

                        PH.Controls.Add(new LiteralControl("&nbsp"));
                        PH.Controls.Add(new LiteralControl("&nbsp"));
                        PH.Controls.Add(new LiteralControl("&nbsp"));
                        TableCell cell1 = new TableCell();

                        DropDownList ddl = new DropDownList();
                        ddl.Width = 100;
                        ddl.Height = 25;
                        ddl.ID = "ddl_" + i;
                        ddl.Items.Add("select");
                        ddl.Items.Add("1");
                        ddl.Items.Add("2");
                        ddl.Items.Add("3");
                        ddl.Items.Add("4");
                        ddl.Items.Add("5");
                       
                       ddl.SelectedValue= "select";
                     

                        PH.Controls.Add(ddl);


                        PH.Controls.Add(cell1);
                     

                    }
                }

                btnAdd.Visible = true;

            }
        }

        
       
       DataTable button_action()                    //action of add button
       {
           try
           {
               DataTable dt1= new DataTable();
               DataTable dt2 = new DataTable();
               DataTable dt3 = new DataTable();
              DataTable  dt4 = new DataTable();
                   

               dt3.Columns.Add("1");
               dt3.Columns.Add("2");
               dt3.Columns.Add("3");
               classCLZ objClass = new classCLZ(cboGrade.SelectedItem.ToString());
               timeTableCLZ objTT = new timeTableCLZ();
              
               dt2 = objClass.add_again(cboGrade.SelectedValue.ToString(),cboSubject.SelectedValue.ToString());

            
            if(dt2.Rows.Count!=0)
             {
               for (int i = 0; i < dt2.Rows.Count; i++)
               {

                   DataRow dr = dt3.NewRow();
                   DropDownList d = (DropDownList)PH.FindControl("ddl_" + i);
                   dr["1"] = d.SelectedValue.ToString();
                   dr["2"] = dt2.Rows[i][0].ToString();
                   dr["3"] = cboSubject.SelectedValue.ToString();
                  
                   dt3.Rows.Add(dr);

               }
             }

               return dt3;
           }

          
           catch(Exception e)
           {
               throw e;
            }
             
               
             
         
           
       }

       protected void cboSubject_SelectedIndexChanged(object sender, EventArgs e)
       {

       }

       protected void btnAdd_Click(object sender, EventArgs e)
       {
            if (ViewState["falg"] == null)
            {
                add_dynamic_control();
            }

            ViewState.Add("falg", true);

            insertCC = new classCLZ();

            if (insertCC.insertClassCombination(button_action()) == "alert('Class Combination is successfully added');")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Class Combination is successfully added')", true);
                ViewState.Clear();
            }

            if (insertCC.insertClassCombination(button_action()) == "alert('Class Combination is already added for this class and subject');")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Class Combination is already added for this class and subject');", true);

            }

            if (insertCC.insertClassCombination(button_action()) == "alert('Please select a group for all classes');")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Please select a group for all classes');", true);
            }

            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Error')", true);
            }
           

       }

     
       
      
      
       
       
    }
}
